package com.yzjs.localhost.service.hrmAssessment.impl;

import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dao.hrmAssessment.HrmAssessmentMapper;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmAssessment.HrmAssessmentEntity;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmAssessment.HrmAssessmentService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("hrmAssessmentService")
public class HrmAssessmentServiceImpl extends BaseServiceImpl<HrmAssessmentEntity, HrmAssessmentMapper > implements HrmAssessmentService {

    @Override
    public ComparisonResultDTO AssessmentCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity beforce = dto.getBeforce();
        String beforceInfo = beforce.getInfo();
        Map<String, Object> beforceParse = JsonUtil.json2map(beforceInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();

        ComparisonResultDTO resultDTO = null;
        if (beforce.getOperatorCode().contains("assessment:add")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 考核备注,考核得分,考核内容,总分,标准分值,用户ID,主键ID
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "amtMemo,amtScore,amtTextCont,amtTotalScore,standardScore,amtUserId,amtId");
        } else if (beforce.getOperatorCode().contains("assessment:edit")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 考核备注,考核得分,考核内容,总分,标准分值,用户ID,主键ID
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "amtMemo,amtScore,amtTextCont,amtTotalScore,standardScore,amtUserId,amtId");
        } else if (beforce.getOperatorCode().contains("assessment:remove")) {
            beforceParse.put("delFlag", "2");
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对 用户状态
            resultDTO = AnalysisService.comparisonMap(beforce.getOperatorCode(), beforceParse, afterParse, "amtIds,delFlag");
            HrmAssessmentEntity hrmAssessmentEntity = new HrmAssessmentEntity();
            hrmAssessmentEntity.setAmtId(Long.valueOf((String) afterParse.get("amtIds")));
            hrmAssessmentEntity.setDelFlag((String) afterParse.get("delFlag"));
            hrmAssessmentEntity.setDataIdentification(after.getDataIdentification());
            this.insert(hrmAssessmentEntity);
            return resultDTO;
        }

        // 记录表表信息
        HrmAssessmentEntity assessmentEntity = JsonUtil.json2pojo(after.getInfo(), HrmAssessmentEntity.class);
        assessmentEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        assessmentEntity.setDataIdentification(after.getDataIdentification());
        this.insert(assessmentEntity);

        return resultDTO;
    }
}